package com.ff.xyh.common.utils;

/**
 * @Auther FF
 * @Date 2022年 11月 23日
 * @Time 03:21
 */

public class MyNodeList {
    private Node head;
    public MyNodeList (){
        this.head = new Node();
    }

    public MyNodeList add(Node node, int data) {
        this.head.add(node, data);
        return this;
    }
    public MyNodeList add( int from, int... datas) {
        this.head.add(this.getHead(), from, datas);
        return this;
    }
    public Node getHead() {
        return this.head;
    }
    public void ListAll() {
        this.head.ListAll();
    }
    public MyNodeList reverse() {
        this.head.reverse();
        return this;
    }

    private class Node {
        int data;
        Node next;
        private Node() {
        }

        protected Node add (Node anyNode, int data) {
            System.out.println("single param");
            Node node = new Node();
            node.data = data;
            if (anyNode.next != null) {
                node.next = anyNode.next;
            }
            anyNode.next = node;
            return node;
        }

        private Node add (Node head, int from, int... datas) {
            if (datas.length <= 0)
                throw new IllegalArgumentException();
            System.out.println("protocol params");
            if (from > this.getSize() || from < 0)
                throw new IndexOutOfBoundsException();
            int count = 0;
            Node temp = head;
            while (count < from){
                temp = temp.next;
            }
            Node lastNode = temp.add(temp, datas[0]);
            for (int i = 1 ; i < datas.length; i++){
                lastNode = lastNode.add(lastNode, datas[i]);
            }
            return lastNode;
        }

//        private Node add (Node anyNode, int... datas) {
//            System.out.println("protocol params");
//            Node temp = anyNode.next;
////            if (temp != null) {
////                while (temp.next != null){
////                    temp = temp.next;
////                }
////            }
//            Node lastNode = temp.add(temp, datas[0]);
//            for (int i = 1 ; i < datas.length; i++){
//                lastNode = lastNode.add(lastNode, datas[i]);
//            }
//            return lastNode;
//        }
        private Node reverse () {
//            this 就是头 头要往后移动
            Node p1 = null;
            Node p2 = null;
            Node tmp = null;
            while (this.next != null) {
                p1 = this.next;
                tmp = this.next.next;
                p1.next = p2; // 此时 this.next已经是p1.next 而不是原本的this.next.next 所以需要借助临时变量
                p2 = tmp;
                if (p2 == null) {
                    this.next = p1;
                    break;
                }
                this.next = p2.next;
                p2.next = p1;
            }
            if (this.next != p1) {
                System.out.println("head.next != p1");
                this.next = p2;
                return this;
            } else {
                System.out.println("head.next == p1");
                return this;
            }

        }
        private void ListAll () {
            Node temp = this.next;
            while (temp != null) {
                System.out.println(temp.data);
                temp = temp.next;
            }
        }
        private int getSize () {
            int count = 0;
            Node temp = this.next;
            while (temp != null) {
                count++;
                temp = temp.next;
            }
            return count;
        }

    }

    public static void main(String[] args) {
//        Node node = new Node();
        MyNodeList nodeList = new MyNodeList();
        Node head = nodeList.getHead();
        nodeList.add(1,2,3,3,4,4,45,5,5,5,5,56,6,6,6,654,56,456,465,456,888);
        nodeList.add(nodeList.getHead(), 18);
        nodeList.ListAll();

//            .add(1)
//            .add(2)
//            .add(3)
//            .add(4)
//            .add(6)
//            .add(8)
//            .add(9)
//            .add(10)
//            .add(31)
//            .add(12);
//        head.ListAll();
//        head.reverse();
//        head.ListAll();

    }
}

